// pages/follow/follow.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')
Page({

  data: {
    host:app.globalData.host,
    TabCur: 0,
    type:1,
    modalName:'',
    scrollLeft:0,
    tabList:[
      {
        name:'图文', 
        type:1,
      },
      {
        name:'分享', 
        type:3,
      },
      {
        name:'提问', 
        type:2,
      },
    ]
  },
  showModal:function(e){
    this.setData({
      modalName: 'modal1',
      collect_id:e.currentTarget.dataset.id
    })
  },
  showModal2:function(e){
    this.setData({
      modalName: 'modal2',
      nofollow_parent_id:e.target.dataset.parent_id
    })
  },
  hideModal:function(){
    this.setData({
      modalName: '',
    })
  },

  changeFollow:function(parent_id,child_id,status){
    var data = this.data.data;
    if(status == 1){
      for(var i =0 ;i< data.length;i++){
        if(data[i].parent_id == parent_id && data[i].child_id == child_id ){
          data[i].follow = status;
        }
      }
    }
    if(status == 0){
      for(var i =0 ;i< data.length;i++){
        if(data[i].parent_id == parent_id ){
          data[i].follow = status;
        }
      }
    }
    this.setData({
      data:data
    });
  },

  /**
   * 关注
   * @param {*} e 
   */
  follow:function(e){
    var data = e.target.dataset, parent_id =data.parent_id,child_id = data.child_id,index = data.index;
    var that = this;
    var row = 'data['+index+'].follow';
    util.request('follow/add', {type:1,parent_id:parent_id,child_id:child_id}, function (data) {
      console.log(data);
      if(data.code == 1){
       that.changeFollow(parent_id,child_id,1);
        wx.showToast({
          title: '关注成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  /**
   * 取消关注
   * @param {*} e 
   */
  nofollow:function(e){
    var parent_id = this.data.nofollow_parent_id;
    var that = this;
    util.request('follow/delete', {type:1,parent_id:parent_id}, function (data) {
      console.log(data);
      if(data.code == 1){
        that.setData({
          nofollow_parent_id:''
        })
        that.changeFollow(parent_id,'',0);
        that.hideModal();
        wx.showToast({
          title: '操作成功',
          icon: 'success',
          duration: 1500
        })
      }else{
        that.setData({
          nofollow_parent_id:''
        })
        util.fail(data.msg,that);
      }
    });
  },


  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
    this.setData({
      type:this.data.tabList[this.data.TabCur].type
    })
    this.getData();
  },


  /**
   * 收藏/取消收藏
   * @param {*} e 
   */
  collect:function(e){
    var that = this;
    util.request('collect/save', {related_id:that.data.collect_id}, function (data) {
      if(data.code == 1){
        that.hideModal();
        that.setData({
          collect_id:''
        })
        setTimeout(function(){
          that.getData();
        },500)
      }else{
        that.setData({
          collect_id:''
        })
        that.hideModal();
        util.fail(data.msg,that);
      }
    });
  },



  /**
   * 点赞/取消点赞
   * @param {*} e 
   */
  thumb:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.data[index],that = this;
    var thumb = data.thumb;
    util.request('thumb/save', {type:3,related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'data['+index+'].thumb';
        if(thumb.is_thumb > 0){
          that.setData({
              [row]:{
                count:thumb.count-1,
                is_thumb:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:thumb.count+1,
              is_thumb:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },



  ViewImage(e) {
    var array = this.data.data[e.currentTarget.dataset.index].image_array,arr = [];
   
    for(var i = 0;i<array.length;i++){
      arr[i] = this.data.host+array[i];
    }
    wx.previewImage({
      urls: arr,
      current: e.currentTarget.dataset.url
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //内容列表
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


  getData: function() {
    var that = this;
    that.setData({
      tips:{
        show_loading:true,
        no_data:false,
      }
    })
    util.request('collect/index', {type:that.data.type}, function (data) {
      if(data.code == 1){
        that.setData({
          data:data.data.data,
        });
        that.setData({
          tips:{
            show_loading:false,
            no_data:true,
          }
        })
      }else{
        that.setData({
          tips:{
            show_loading:false,
            no_data:false,
          }
        })
        util.fail(data.msg,that);
      }
    });
  },
})